<?php
class category
{
	private $category; 	private $detail_cate; 	private $show_cate; 	private $paren_cate; private $offset; public $countr;
	 public $obj;
	function set($c,$d,$s,$p,$ost='null')
	{
		$p_o=explode("@",$p);
		$this->category=addslashes(strip_tags(trim($c)));
		$this->detail_cate=addslashes(strip_tags(trim($d)));
		$this->show_cate=$s;
		$this->parent_cate=$p_o[0];
		if($ost=='null')
		$this->offset=$p_o[1];
		else
		$this->offset=$ost;
	}
	function isNotUnique($cate,$oldcate='null')
	{
		if($oldcate=='null')
		$sql = "SELECT categoryId FROM sme_category WHERE category='$cate' "; 
		else 
		$sql = "SELECT categoryId FROM sme_category WHERE category='$cate' AND category<>'$oldcate' "; 
		$rs=mysql_query($sql);
		$row=mysql_num_rows($rs);
		if($row==0)return TRUE;
		else return FALSE;
	}
	function setshow($cateid,$sh)
	{
		$sql = "UPDATE sme_category SET show_cate = '$sh' WHERE categoryId = $cateid ";
		if(mysql_query($sql)) return TRUE;
		else return FALSE;
	}
	function insert()
	{
		if(!empty($this->category)&& !empty($this->offset)){
			$sql = "INSERT INTO sme_category 
			VALUES (NULL, '$this->category', '$this->detail_cate', '$this->show_cate', '$this->parent_cate','$this->offset')"; 
		
			if(mysql_query($sql)) return TRUE;
			else return FALSE;
		}else return FALSE;
	}
	function update($idcate)
	{
		if(!empty($idcate) && !empty($this->category)){
			$sql = "UPDATE sme_category SET 
			category = '$this->category', 
			detail_cate = '$this->detail_cate',
			show_cate = '$this->show_cate',
			parent_cate = '$this->parent_cate',
			offset = '$this->offset'
			WHERE categoryId = $idcate "; 
			
			if(mysql_query($sql)) return TRUE;
			else return FALSE;}
		else return FALSE;
	}
	function detete($idcate)
	{
		if(!empty($idcate) && $this->chOnDel($idcate)){
			$sql="DELETE FROM sme_category WHERE categoryId = $idcate ";
			if(mysql_query($sql)) return TRUE;
			else return FALSE;}
		else return FALSE;
	}
	private function chOnDel($idcate)
	{
		$sql="SELECT productId,categoryId FROM sme_product, sme_category 
				WHERE proCategoryId =$idcate OR parent_cate =$idcate";
				$rs=mysql_query($sql);
		$row=mysql_num_rows($rs);
		if($row==0)return TRUE;
		else return FALSE;
	}
	function getCategory()
	{
		$sqllist="SELECT categoryId,category,detail_cate,show_cate,parent_cate,offset,count( productId )
					FROM sme_category
					LEFT JOIN sme_product ON proCategoryId = categoryId  AND sme_product.delete <> 1
					GROUP BY categoryId
					ORDER BY offset ASC ,categoryId ASC"; 
		$rslist=mysql_query($sqllist);
		$i=0;
		
		 while($list = mysql_fetch_array($rslist)) 
		 {
			 $cate[$i][0]=$list[0];
			 $cate[$i][1]=$list[1];
			 $cate[$i][2]=$list[2];
			 $cate[$i][3]=$list[3];
			 $cate[$i][4]=$list[4];
			 $cate[$i][5]=$list[5];
			 $cate[$i][6]=$list[6];
			 $i++;
		 }
		 $this->countr=$i-1;
		$this->obj=$cate;
	}
	function getRecord($catid)
	{
		$sql="SELECT * FROM sme_category WHERE categoryId=$catid";
		$rs=mysql_query($sql);
		$list = mysql_fetch_array($rs);
		return $list;
	}
	function haveRow($catid)
	{
		
		$sql="SELECT * FROM sme_category WHERE categoryId=$catid";
		$rs=mysql_query($sql);
		$row=mysql_num_rows($rs);
		if($row>0)return TRUE;
		else return FALSE;
	
	}
}
?>